package com.hxx.common.core.wcontour;

import com.alibaba.fastjson.JSON;
import com.hxx.common.core.wcontour.db.PgDataStore;
import com.hxx.common.core.wcontour.util.*;
import com.hxx.common.core.wcontour.base.TrainData;
import com.hxx.common.core.wcontour.enums.TempColorEnum;
import com.hxx.common.core.wcontour.read.TempJsonUtil;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.SchemaException;
import org.geotools.map.Layer;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class TestTemp {

    public static void main(String[] args) throws IOException, SchemaException {
        // 气象温度json数据,等值面裁切shp文件路径
        String jsonFile = "/Users/***/工作/绘天/气象项目/温度12小时/2022090116.json";
//        String boundryFile = "/Users/***/test/border_line/440000_borderline.shp";
        String boundryFile = "/Users/***/Downloads/地市边界线/地市边界线.shp";
        // 数据分层级别(数据间隙)
        double[] dataInterval = TempColorEnum.getValueArray();
        FeatureSurfaceUtil equiSurface = new FeatureSurfaceUtil();
        // 读取气象json文件，返回二维数组温度站点经纬度及温度值
        TrainData trainData = TempJsonUtil.readJson(new File(jsonFile));
        int[] size = trainData.getSize();
        System.out.println(String.format("开始进行等值面分析，经纬度个数和温度值数量为：%s, %s", size[0], size[1]));

        long start = System.currentTimeMillis();
        FeatureCollection featureCollection = equiSurface.equiSurface(trainData, dataInterval);
        // 广东省裁切
        featureCollection = FeatureClipUtil.clipFeatureCollection(featureCollection, boundryFile);
        System.out.println("等值面分析完成，总花时：" + (System.currentTimeMillis() - start));

        // 数据入库
        FeatureWriteDbUtil.write(featureCollection, PgDataStore.getDatastore(), "layer_temp");

        // 添加样式 透明度，颜色级别
        start = System.currentTimeMillis();
        float opacity = 1f;
        Map<Double, String> levelProps = TempColorEnum.getValueColorMap();
        Layer layer = FeatureUtil.addShapeLayer(featureCollection, levelProps, opacity);
        System.out.println("添加样式，总花时：" + (System.currentTimeMillis() - start));

        // 输出图片
        String imgPath = "/Users/***/test/test/temp.png";
        // 获取shp边界，生成格网点
        double minX = 106.55;
        double minY = 17.695;
        double maxX = 119.75;
        double maxY = 28.005;
        double[] bbox = {minX, minY, maxX, maxY};
        int[] _xy = {1000, 800};
        GeoImageUtil.layerToPng(layer, bbox, _xy, imgPath);
        System.out.println("输出图片完成");

        // 输出geoJson
        String geojson = "/Users/***/test/train/temp_440000.json";
        JSON geoJson = FeatureUtil.featureToGeoJson(featureCollection);
        Files.writeString(Paths.get(geojson), geoJson.toJSONString());
        System.out.println("输出geoJson完成");
    }
}
